import Model from "./Model";
import WxUtil from "./WxUtil";
import Config from "./Config";

let wx = window['wx']
const { ccclass, property } = cc._decorator;

@ccclass
export default class GameEnd extends cc.Component {

    @property(cc.Node)
    winNode: cc.Node = null;

    @property(cc.Node)
    loseNode: cc.Node = null;

    @property(cc.Node)
    topNode: cc.Node = null;

    @property([cc.Node])
    stars: cc.Node[] = [];

    @property(cc.Node)
    eff: cc.Node = null;

    @property(cc.Prefab)
    endParticle: cc.Prefab = null;

    @property({
        type:[cc.AudioClip]
    })
    starAudios: cc.AudioClip[] = [];

    onLoad() {
        this.winNode.active = Model.game.win
        this.loseNode.active = !Model.game.win

        this.topNode.getChildByName('success').active = Model.game.win
        this.topNode.getChildByName('failed').active = !Model.game.win


        //动态调整top位置
        var size = cc.view.getVisibleSize();
        if (size.height / size.width > 2) {
            this.topNode.getComponent(cc.Widget).top = 160
        } else {
            this.topNode.getComponent(cc.Widget).top = 110
        }

        if (!Model.game.win) {
            this.eff.active = true;
            var fade = cc.fadeOut(1);
            var call = cc.callFunc(this.onEffend, this);
            var seq = cc.sequence(fade, call);
            this.eff.runAction(seq);
        }
        if(WxUtil.gridAdLoaded)
        {
            WxUtil.showGridAd();
        }else
        {
            WxUtil.showBanner();
        }
    }
    onEffend() {
        this.eff.active = false
    }

    start() {

        if (Model.game.selectedModel != 4) {
            this.topNode.getChildByName('star1').active = false;
            this.topNode.getChildByName('star2').active = false;
            this.topNode.getChildByName('star3').active = false;
            this.topNode.getChildByName('stargray').active = false;
            return;
        }

        if(!Model.game.win)return;

        if(Model.game.selectedLevel%2==0)
        {
            WxUtil.showInterstitialAd();
        }


        let getStar = Model.game.getStar;
        for (var i = 0; i < getStar; i++) {
            var star = this.stars[i];
            star.active = true
            star.scale = 4;
            star.opacity = 0
            var t = i * 0.3 + 0.3
            var delay = cc.delayTime(t);
            var scale = cc.scaleTo(0.5, 1, 1).easing(cc.easeBackOut());
            var seq = cc.sequence(delay, scale);
            star.runAction(seq);

            var delay2 = delay.clone();
            var fade = cc.fadeIn(0.3)
            var call = cc.callFunc(this.creatExpEffect, this, [i, star.x, star.y]);
            star.runAction(cc.sequence(delay2, fade, call))
        }
        //提交分数
        var starC = 0
        for (var i = 0; i < Model.game.drawLevelInfo.length; i++) {
            if (Model.game.drawLevelInfo[i] > 0)
                starC += Model.game.drawLevelInfo[i];
        }
        if (typeof wx === 'undefined') {
            return;
        }
        // 发送结算分数到开放域
        wx.getOpenDataContext().postMessage({
            message: starC
        });

    }
    creatExpEffect(obj, data) {
        // this.particles[data[0]].resetSystem();
        var particle = cc.instantiate(this.endParticle);
        particle.getComponent(cc.ParticleSystem).resetSystem()
        particle.x = data[1]
        particle.y = data[2]
        this.node.getChildByName('top').addChild(particle);

        cc.audioEngine.playEffect(this.starAudios[data[0]], false)
    }
    next() {

        if (Model.game.selectedModel == 1) {
            if (Model.game.selectedLevel >= Config.preciseLevels.length - 1)
                cc.director.loadScene('menu')
            else {
                Model.game.selectedLevel++
                cc.director.loadScene('game')
            }
        }
        else if (Model.game.selectedModel == 2) {
            // console.log(Model.game.selectedLevel, Config.brickLevels.length - 1)
            if (Model.game.selectedLevel >= Config.brickLevels.length - 1)
                cc.director.loadScene('menu')
            else {
                Model.game.selectedLevel++
                cc.director.loadScene('gameDrop')
            }
        }
        else if (Model.game.selectedModel == 3) {
            if (Model.game.selectedLevel >= Config.dragLevels.length - 1)
                cc.director.loadScene('menu')
            else {
                Model.game.selectedLevel++
                cc.director.loadScene('gameDrag')
            }
        }
        else if (Model.game.selectedModel == 4) {
            //console.log(Model.game.selectedLevel, Config.drawLevels.length - 1)
            if (Model.game.selectedLevel >= Config.drawLevels.length - 1)
                cc.director.loadScene('menu')
            else {
                Model.game.selectedLevel++
                cc.director.loadScene('gameDraw')
            }
        }
    }
    retry() {
        if (Model.game.selectedModel == 1) {
            if (Model.game.selectedLevel >= Config.preciseLevels.length - 1)
                cc.director.loadScene('menu')
            else {
                cc.director.loadScene('game')

            }
        }
        else if (Model.game.selectedModel == 2) {
            // console.log(Model.game.selectedLevel, Config.brickLevels.length - 1)
            if (Model.game.selectedLevel >= Config.brickLevels.length - 1)
                cc.director.loadScene('menu')
            else {
                cc.director.loadScene('gameDrop')
            }
        }
        else if (Model.game.selectedModel == 3) {
            if (Model.game.selectedLevel >= Config.dragLevels.length - 1)
                cc.director.loadScene('menu')
            else
                cc.director.loadScene('gameDrag')
        }
        else if (Model.game.selectedModel == 4) {
            //console.log(Model.game.selectedLevel, Config.drawLevels.length - 1)
            if (Model.game.selectedLevel >= Config.drawLevels.length - 1)
                cc.director.loadScene('menu')
            else {

                cc.director.loadScene('gameDraw')
            }
        }
    }
    share() {
        WxUtil.share()
    }
    onXmxxClick()
    {
        WxUtil.jump("wxdb956f27b88772cc")
    }
    onZumaClick()
    {
        WxUtil.jump("wx6dac4c6e1e91c454")
    }
    onSuduClick()
    {
        WxUtil.jump("wxa45ec57d5717db87")
    }
    onBrickClick()
    {
        WxUtil.jump("wx570fee9777848176")
    }
    onBackClick()
    {
        cc.director.loadScene('menu')
    }
    // update (dt) {}
    onDestroy()
    {
        WxUtil.destroyBanner()
        WxUtil.destroyGrid()
    }
}
